package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 山东舜网新闻 解析类
 *
 * @author Chase
 *
 */
public class E23NewsAnalyse implements AnalyseNews {

	public static void main(String[] args) {
		E23NewsAnalyse parser=new E23NewsAnalyse();
		String Url="http://news.e23.cn/content/2014-01-13/2014011300401.html";
		UrlMeta oneMeta=CrawlHTML.responseToURL(Url);
		System.out.println(parser.parserHtml(oneMeta));
	}

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://news[.]e23[.]cn/content/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*.html",
				"http://baby[.]e23[.]cn/content/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*.html",
				"http://lady[.]e23[.]cn/content/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*.html",
				"http://mall[.]e23[.]cn/content/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*.html",
				"http://health[.]e23[.]cn/content/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*.html",
				"http://nx[.]e23[.]cn/content/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*.html",
				"http://money[.]e23[.]cn/content/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*.html",
				"http://car[.]e23[.]cn/content/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*.html",
				"http://opinion.e23.cn/user1/[0-9]*/archives/[0-9]{4}/[0-9]*.html",
				"http://e.e23.cn/content/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*.html",
				"http://house.e23.cn/content/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*.html",
				"http://home.house.e23.cn/content/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*.html",
				"http://travel.e23.cn/content/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*.html",
				"http://www.sdstech.cn/content/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*.html",
				"http://.*.e23.cn/.*/[0-9]{4}-[0-9]{2}-[0-9]{2}/[0-9]*.html"
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String url=urlMeta.getUrl();

		String html=urlMeta.getHtml();

		/*
		 * 定义新闻实体需要的字段
		 */
		String title=null;
		String content=null;
		Long dateLong=null;
		String commentNum=null;
		String clickNum=null;

		Document doc=Jsoup.parse(html);
		doc.select(".shengming").remove();
		if(url.startsWith("http://opinion.e23.cn")){
			title=doc.select("span.ah14").text();// 标题
			dateLong=DateUtils.matchDate(doc.select("div.p14").text());// 时间
			content=HtmlCleaner.getContentHtml(url,doc.select("td.p14")); //内容
//			commentNum=doc.select("span#DRed tieTotalCount").text(); //评论数
		} else {
			title=doc.select("div.news_container>h1").text();// 标题
			dateLong=DateUtils.matchDate(doc.select("div.artInfo").text());// 时间
			content=HtmlCleaner.getContentHtml(url,doc.select("div.h16")); //内容
//			commentNum=doc.select("span#DRed tieTotalCount").text(); //评论数
			if(StringUtils.isNullOrEmpty(content)) {
				content=HtmlCleaner.getContentHtml(url,doc.select(".iksFMOWX17")); //内容
			}
			if(StringUtils.isNullOrEmpty(content)) {
				content=HtmlCleaner.getContentHtml(url,doc.select("div#endSummary")); //内容
			}
		}
		if (dateLong==null){
			dateLong=DateUtils.matchDate(url);
		}

		if(StringUtils.isBlank(title)){
			title=doc.select("h1").text();
		}

		NewsMeta meta=new NewsMeta();
		meta.setTitle(title);
		meta.setUrl(url);
		meta.setType(1);
		meta.setClickNum(NumberUtils.parseInt(clickNum));
		meta.setCommentNum(NumberUtils.parseInt(commentNum));
		meta.setContent(content);
		meta.setDate(dateLong);

		String author= JSoupUtils.matchAuthor(doc, "来源:");
		meta.setAuthor(author);
		return meta;
	}



	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
